package com.neuedu.controller.dept;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neuedu.common.ResultBean;
import com.neuedu.factory.ClassFactory;
import com.neuedu.service.TableService;
import com.neuedu.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet(name = "QueryDeptServlet", urlPatterns = "/his/querydept")
public class QueryDeptServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String deptnum = request.getParameter("deptnum");
        String deptname = request.getParameter("deptname");
        deptnum = "%" + deptnum + "%";
        deptname = "%" + deptname + "%";
        Object[] objs = {deptnum, deptname};
        ObjectMapper om = new ObjectMapper();
        TableService dcs = (TableService) ClassFactory.createObj("TableService");
        List<Map<String, Object>> list = dcs.likeQuery(objs);
        ResultBean rb = new ResultBean();
        rb.setList(list);
        if (list.size() > 0) {
            rb.setCount(list.size());
        } else {
            rb.setCount(0);
        }
        String s = om.writeValueAsString(rb);
        PrintWriter writer = response.getWriter();
        writer.append(s);
    }
}
